﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Entity;

namespace DAL
{
   public class CpglDAL
    {
        /// <summary>
        /// 插入一条操盘攻略
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool InsertCpgl(Cpgl model)
        {
            try
            {
                Tools tool = new Tools();
                tool.AddSqlParameter("Name", SqlDbType.VarChar, model.Name);
                tool.AddSqlParameter("Link", SqlDbType.VarChar, model.Link);
                int rValue = SqlHelper.ExecuteNonQuery(DBManager.WebSite, System.Data.CommandType.StoredProcedure, "InsertCpgl", tool.GetSqlParameters());
                if (rValue > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception exsql)
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(Cpgl model)
        {
            try
            {
                Tools tool = new Tools();
                tool.AddSqlParameter("id", SqlDbType.Int, model.Id);
                tool.AddSqlParameter("Name", SqlDbType.VarChar, model.Name);
                tool.AddSqlParameter("Link", SqlDbType.VarChar, model.Link);
                int rValue = SqlHelper.ExecuteNonQuery(DBManager.WebSite, System.Data.CommandType.StoredProcedure, "[UpdateCpgl]", tool.GetSqlParameters());
                if (rValue > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception exsql)
            {
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                Tools tool = new Tools();
                tool.AddSqlParameter("id", SqlDbType.Int, id);
                int rValue = SqlHelper.ExecuteNonQuery(DBManager.WebSite, System.Data.CommandType.StoredProcedure, "DeleteCpgl", tool.GetSqlParameters());
                if (rValue == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception exsql)
            {
                return false;
            }

        }

        /// <summary>
        /// 得到所有的成功启示录
        /// </summary>
        /// <returns></returns>
        public List<Cpgl> GetTop50()
        {
            DataTable dt = SqlHelper.ExecuteGetTable(DBManager.WebSite, System.Data.CommandType.StoredProcedure, "GetRxtCpgl", null);
            List<Cpgl> lstSuccess = new List<Cpgl>();
            foreach (DataRow reader in dt.Rows)
            {
                Cpgl success = new Cpgl();
                success.Id = Tools.GetInt(reader["Id"]);
                success.Name = Tools.GetString(reader["Name"]);
                success.Link = Tools.GetString(reader["Link"]);
                lstSuccess.Add(success);
            }
            return lstSuccess;
        }

        public Cpgl GetElementById(int id)
        {
            Tools tool = new Tools();
            tool.AddSqlParameter("id", SqlDbType.Int, id);
            DataTable tb = SqlHelper.ExecuteGetTable(DBManager.WebSite, CommandType.StoredProcedure, "GetCpglById", tool.GetSqlParameters());
            Cpgl s = null;
            if (tb.Rows.Count > 0)
            {
                s = new Cpgl();
                DataRow row = tb.Rows[0];
                s.Id = Tools.GetInt(row["Id"]);
                s.Name = Tools.GetString(row["Name"]);
                s.Link = Tools.GetString(row["Link"]); ;
            }
            return s;
        }
    }
}
